import pulp

# 定义物品的重量和价值
weights = [2, 3, 4, 5]
values = [3, 4, 5, 6]
capacity = 5
num_items = len(weights)

# 创建一个线性规划问题
lp_problem = pulp.LpProblem("Knapsack Problem", pulp.LpMaximize)

# 定义决策变量，item2和item3为连续变量，其余为二进制变量
x = [pulp.LpVariable(f'x{i}', cat='Binary') if i not in [2, 3] else pulp.LpVariable(f'x{i}', lowBound=0, upBound=1, cat='Continuous') for i in range(num_items)]

# 定义目标函数：最大化总价值
lp_problem += pulp.lpSum([values[i] * x[i] for i in range(num_items)])

# 定义约束条件：总重量不能超过背包容量
lp_problem += pulp.lpSum([weights[i] * x[i] for i in range(num_items)]) <= capacity

# 额外约束条件
# 1. 最小价值约束：总价值必须至少达到5
lp_problem += pulp.lpSum(x[0]) >= 0.5*(x[1]+x[2])

# 2. 特定物品组合约束：如果选择了物品0，则必须选择物品1
lp_problem += x[0] <= x[1]

# 3. 特定物品排他约束：如果选择了物品2，则不能选择物品3
lp_problem += x[2] + x[3] <= 1

# 4. 等式约束：物品0和物品1的总重量必须等于3
lp_problem += weights[0] * x[0] + weights[1] * x[1] == 3

# 求解问题
lp_problem.solve()

# 输出结果
print("Status:", pulp.LpStatus[lp_problem.status])
print("Optimal Solution to the problem:")
for i in range(num_items):
    print(f'Item {i}:', pulp.value(x[i]))

# 输出最大价值
print("Maximum Value:", pulp.value(lp_problem.objective))